﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PressureTest
{
    internal class SqlSugarHelper
    {
        static Config config = new Config();

        /// <summary>数据库联接字符串
        /// 
        /// </summary>
        static string connectionString = string.Format("Database={0};Host ={1};Port={2};Username={3};Password={4};SslMode=none;AllowPublicKeyRetrieval=True", config.DBName, config.DBHost, config.DBPort, config.DBUser, config.DBPwd);

        /// <summary>错误代码
        /// 
        /// </summary>
        static int ErrorCode = -1;

        /// <summary>SQL工具
        /// 
        /// </summary>
        public static SqlSugarScope DB = new SqlSugarScope(new ConnectionConfig()
        {

            ConnectionString = connectionString,//连接符字串
            DbType = DbType.MySql,//数据库类型
            IsAutoCloseConnection = true //不设成true要手动close
        },
            db => {
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    Debug.WriteLine(sql);//输出sql,查看执行sql 性能无影响
                };
                db.Aop.OnError = (exp) =>//SQL报错
                {

                    Debug.WriteLine("数据库连接错误" + exp.ToString());
                };
            }
         );

        /// <summary>连接状态
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool State()
        {
            Debug.WriteLine(connectionString);
            return DB.Ado.IsValidConnection();
        }

        /// <summary>插入数据
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Insertable<T>(List<T> list) where T : class, new()
        {
            try
            {
                int res = DB.Insertable(list).ExecuteCommand();
                return res;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("插入记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>删除记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <returns></returns>
        public static int Deleteable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Deleteable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("删除记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>更新记录
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Updateable<T>(List<T> list) where T : class, new()
        {
            try
            {
                return DB.Updateable<T>(list).ExecuteCommand();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("更新记录异常：" + ex.Message);
                return ErrorCode;
            }
        }

        /// <summary>查询符合条件的记录
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="where"></param>
        /// <returns></returns>
        public static List<T> Inquireable<T>(Dictionary<string, Object> where)
        {
            try
            {
                ISugarQueryable<T> sugarQueryable = null;
                if (where != null)
                {
                    sugarQueryable = DB.Queryable<T>().WhereColumns(where);
                }
                else
                {
                    sugarQueryable = DB.Queryable<T>();
                }
                List<T> list = sugarQueryable.ToList();
                return list;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("查询记录异常：" + ex.Message);
                return new List<T>();
            }
        }
    }
}

[SugarTable("p_device")]
public class Device
{
    //数据是自增需要加上IsIdentity 
    //数据库是主键需要加上IsPrimaryKey 
    //注意：要完全和数据库一致2个属性
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int id { get; set; }

    public int number { get; set; }
    
    public string name { get; set; }

    public int address { get; set; }

    public string port { get; set; }

    public int baudRate { get; set; }
}

[SugarTable("p_device_point")]
public class DevicePoint
{
    //数据是自增需要加上IsIdentity 
    //数据库是主键需要加上IsPrimaryKey 
    //注意：要完全和数据库一致2个属性
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int id { get; set; }

    public int device_number { get; set; }

    public string device_name { get; set; }

    public int device_address { get; set; }

    public int point_number { get; set; }

    public string point_name { get; set; }

    public int address { get; set; }

    public int outMin { get; set; }

    public int outMax { get; set; }

    public int rangeMin { get; set; }

    public int rangeMax { get; set; }

    public string unit { get; set; }

    public float amend { get; set; }

    public int selected { get; set; }

    public int alarm_hight { get; set; }

    public int alarm_low { get; set; }

    public int error_range { get; set; }
}

[SugarTable("p_form_point")]
public class FormPoint
{
    //数据是自增需要加上IsIdentity 
    //数据库是主键需要加上IsPrimaryKey 
    //注意：要完全和数据库一致2个属性
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int id { get; set; }

    public int number { get; set; }

    public string name { get; set; }

    public int device_point_number { get; set; }

    public string device_point_name { get; set; }
}

[SugarTable("p_template")]
public class Template
{
    //数据是自增需要加上IsIdentity 
    //数据库是主键需要加上IsPrimaryKey 
    //注意：要完全和数据库一致2个属性
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int id { get; set; }

    public int number { get; set; }

    public string pipe_position { get; set; }

    public float design_pressure { get; set; }

    public int edit_enable { get; set; }

    public float first_stage_pressure { get; set; }

    public int first_stage_minute { get; set; }

    public int first_stage_estimate { get; set; }

    public float first_stage_estimate_value { get; set; }

    public float second_stage_pressure { get; set; }

    public int second_stage_minute { get; set; }

    public int second_stage_estimate { get; set; }

    public float second_stage_estimate_value { get; set; }

    public float third_stage_pressure { get; set; }

    public int third_stage_minute { get; set; }

    public int third_stage_estimate { get; set; }

    public float third_stage_estimate_value { get; set; }

    public float rigor_stage_pressure { get; set; }

    public int rigor_stage_minute { get; set; }

    public int rigor_stage_estimate { get; set; }

    public float rigor_stage_estimate_value { get; set; }

    public float intensity_stage_pressure { get; set; }

    public int intensity_stage_minute { get; set; }

    public int intensity_stage_estimate { get; set; }

    public float intensity_stage_estimate_value { get; set; }
}

[SugarTable("p_project")]
public class Project
{
    //数据是自增需要加上IsIdentity 
    //数据库是主键需要加上IsPrimaryKey 
    //注意：要完全和数据库一致2个属性
    [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
    public int id { get; set; }

    public string number { get; set; }

    public string name { get; set; }

    public string stake_mark { get; set; }

    public string pipe_specification { get; set; }

    public string pipe_length { get; set; }

    public string medium { get; set; }

    public string design_pressure { get; set; }

    public string pipe_nature { get; set; }

    public string test_type { get; set; }

    public string company { get; set; }

    public string manometer_precision { get; set; }

    public string manometer_range { get; set; }

    public int template_id { get; set; }
    
}